package com.hxd.otc.module.order.presenter;

import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.my.model.UploadImageModel;
import com.hxd.otc.module.order.bean.CheckImageBean;
import com.hxd.otc.module.order.bean.ComplainBean;
import com.hxd.otc.module.order.contract.ComplainContract;
import com.hxd.otc.module.order.model.ComplainModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.AliyunUtils;
import com.hxd.otc.utils.LogUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 包  名 :  com.hxd.otc.mvp.deal
 * 类  名 :  ComplainPresenter
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/8 10:14
 * 描  述 :  ${TODO} 投诉P
 */
public class ComplainPresenter extends BasePresenter<ComplainContract.View> implements ComplainContract.Presenter, ComplainModel.Callback {
    private ComplainModel mModel;
    AliyunUtils aliyunUtils;
    private final UploadImageModel mUploadImageModel;

    public ComplainPresenter(ComplainContract.View view) {
        super(view);
        this.mModel = new ComplainModel(this);
        aliyunUtils = new AliyunUtils();
        mUploadImageModel = UploadImageModel.getInstance();
    }

    @Override
    public void complain(ComplainBean mBean) {
        mModel.complain(mBean, new BaseObserver() {
            @Override
            public void onNext(String title, Object bean) {
                mView.complainSuccess(0);
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.showToast(errMessage);
                mView.complainSuccess(1);
            }
        }, mView.<BaseBean>bindAutoDispose());
    }

    @Override
    public void upLoadAliyun(final List<File> files) {
        aliyunUtils.setDatas(files, new AliyunUtils.UploadListener() {
            @Override
            public void onUploadComplete(Map<String, Object> success, List<String> failure) {
                if (failure.size() == 0 && success.size() == files.size()) {
                    LogUtils.d("图片上传成功====");
                    mView.upLoadFinished(0, success);
                } else {
                    mView.upLoadFinished(1, success);
                    LogUtils.d("图片上传失败====");
                }
            }
        });
    }

    @Override
    public void getComplainType() {

        mView.getComplainType(mModel.getComplainType());
    }

    @Override
    public void loadProof() {
        mModel.loadProof();
    }

    @Override
    public void uploadImage(String tag, File file) {
        mUploadImageModel.uploadImage(file, new Observer<String>() {

            @Override
            public void onError(Throwable e) {
                mView.uploadImageFail(e.getMessage());
            }

            @Override
            public void onComplete() {
            }

            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(String result) {
                mView.uploadImageSuccess(result);

            }
        }, mView.<String>bindAutoDispose());
    }

    @Override
    public void start() {

    }

    @Override
    public void proofResult(List<CheckImageBean> data) {
        if (mView != null) {
            mView.proofResult(data);
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        if (aliyunUtils != null) {
            aliyunUtils.cancleTasks();
            aliyunUtils = null;
        }
    }
}
